package com.example.ch02_student.adminsDo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.ch02_student.R;
import com.example.ch02_student.entity.CourseVo;
import com.example.ch02_student.utils.ResultVo;
import com.example.ch02_student.utils.UrlBase;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class CourseInfo extends AppCompatActivity {
private OkHttpClient okHttpClient;
private List<Map<String,String>> list;
private ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_info);
        okHttpClient=new OkHttpClient();
        lv=findViewById(R.id.lv);
        init();
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                ///course/deleteCourse
                String cno=list.get(i).get("cno");
                Request build = new Request.Builder().url(UrlBase.URL_BASE+"/course/deleteCourse?cno="+cno).build();
                Call call = okHttpClient.newCall(build);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    }
                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String string = response.body().string();
                        ResultVo resultVo = new Gson().fromJson(string, ResultVo.class);
                        if (resultVo.getCode()==1000){
                            init();
                        }
                    }
                });
                return true;
            }
        });

    }

    //页面初始化
    public void init(){
        Request build = new Request.Builder().url(UrlBase.URL_BASE+"/course/selectCourseAll").build();
        Call call = okHttpClient.newCall(build);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
            }
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                xuanran(string);

            }
        });
    }

    //数据渲染
    public void xuanran(String string){
        CourseVo[] courseVos = new Gson().fromJson(string, CourseVo[].class);
        list=new ArrayList<>();
        Map<String,String> map;
        for (int i = 0; i < courseVos.length; i++) {
            map=new HashMap<>();
            map.put("cno",courseVos[i].getCno());
            map.put("cname",courseVos[i].getCname());
            map.put("term",courseVos[i].getTerm()+"");
            map.put("ccredit",courseVos[i].getCcredit()+"");
            map.put("chour",courseVos[i].getChour()+"");
            map.put("createTime",new SimpleDateFormat("yyyy-MM-dd HH:mm").format(courseVos[i].getCreateTime()));
            List<String> tnames = courseVos[i].getTnames();
            StringBuilder stringBuilder = new StringBuilder();
            for (String tname : tnames) {
                stringBuilder= stringBuilder.append(tname+",");
            }
            map.put("tnames",stringBuilder+"");
            list.add(map);
        }
        final String[] from=new String[]{"cno","cname","term","ccredit","chour","createTime","tnames"};
        final int[] to=new int[]{R.id.cno,R.id.cname,R.id.term,R.id.ccredit,R.id.chour,R.id.createTime,R.id.tname};
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                SimpleAdapter simpleAdapter = new SimpleAdapter(CourseInfo.this,list,R.layout.activity_course_info_items,from,to);
                lv.setAdapter(simpleAdapter);
                Toast.makeText(CourseInfo.this,"success",Toast.LENGTH_SHORT).show();
            }
        });
    }



}
